<?php

namespace App\Admin\Controllers\Common;

use App\Admin\Models\SiteStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Models\CustomerServiceMem;
use App\Admin\Validate\Common\CustomerServiceMem as ValidateCommonCustomerServiceMem;

class CustomerServiceMemController extends Controller
{
    /**
     * 获取客服管理列表
     */
    public function getlist(Request $request)
    {
        $list = CustomerServiceMem::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取客服管理详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = CustomerServiceMem::where(['id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增客服管理
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['mem_name', 'mobile']);

        $Validate = new ValidateCommonCustomerServiceMem;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }



        $data = $req_data;
        $data['create_at'] = time();
        $data['update_at'] = time();
        CustomerServiceMem::create($data);
        return response()->json(['code' => '000', 'msg' => '新增客服成功']);
    }


    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'mem_name', 'mobile']);


        $Validate = new ValidateCommonCustomerServiceMem('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $info = CustomerServiceMem::where([['id', $req_data['id']],['is_del',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        CustomerServiceMem::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑客服成功']);
    }
}
